import KvMotion from "./kv_motion";
import ButtonGroup from "./button_group";
import EventCenter from "./event_center";
import Event from "./event";
import System from "./system";
import Vec2 from "./vec2";
import Movie from "./movie";
import AudioCtrl from "./audio_ctrl";

class a {
  constructor(e, t, n, r, i, s, o?) {
    let u = this;
    (u.name = e),
      (u.motionIndex = t),
      (u.beginFrame = n),
      (u.endFrame = r),
      (u.x = i),
      (u.y = s),
      (u.reversion = typeof o == "boolean" ? o : !1),
      (u.toNodes = []);
  }
  name = null;
  _soundEffect = null;
  soundEffect(e) {
    let t = this;
    return (
      typeof e == "string" && (t._soundEffect = new AudioCtrl(e, !1)), t._soundEffect
    );
  }
  motionIndex = 0;
  beginFrame = 0;
  endFrame = 0;
  reversion = !1;
  x = 0;
  y = 0;
  distance(e) {
    let t = this,
      n = new Vec2(t.x, t.y),
      r = new Vec2(e.x, e.y);
    return n.distance(r);
  }
  toNodes = null;
  addToNodes(e, x?, y?, z?) {
    let t = this;
    for (let n = 0; n < arguments.length; n++) t.toNodes.push(arguments[n]);
  }
  getMotionFrames(e) {
    let t = this,
      n = e[t.motionIndex],
      r = n.frames().slice(t.beginFrame, t.endFrame + 1);
    return t.reversion && (r = r.reverse()), r;
  }
  reverse() {
    let e = this,
      t = new a(
        e.name,
        e.motionIndex,
        e.beginFrame,
        e.endFrame,
        e.x,
        e.y,
        !e.reversion
      );
    return (t.toNodes = e.toNodes.slice()), t;
  }
}

let f = new a("DEF_LOOP", 0, 0, 14, 0, 0),
  l = new a("DEF_TRANSITION", 0, 15, 28, 0, 1),
  c = l.reverse(),
  h = new a("MC1_TRANSITION", 1, 0, 6, 0, 2),
  p = h.reverse(),
  d = new a("MC1_LOOP", 1, 7, 20, 0, 3);
h.soundEffect("sounds/head_1.mp3");
let v = new a("MC2_TRANSITION", 2, 0, 6, 1, 2),
  m = v.reverse(),
  g = new a("MC2_LOOP", 2, 7, 20, 1, 3);
v.soundEffect("sounds/head_2.mp3");
let y = new a("MC3_TRANSITION", 3, 0, 6, 2, 2),
  b = y.reverse(),
  w = new a("MC3_LOOP", 3, 7, 20, 2, 3);
y.soundEffect("sounds/head_3.mp3");
let E = new a("MC4_TRANSITION", 4, 0, 6, 3, 2),
  S = E.reverse(),
  x = new a("MC4_LOOP", 4, 7, 20, 3, 3);
E.soundEffect("sounds/head_4.mp3"),
  f.addToNodes(l),
  l.addToNodes(h, v, y, E),
  c.addToNodes(l, f),
  h.addToNodes(d),
  p.addToNodes(c),
  d.addToNodes(p),
  v.addToNodes(g),
  m.addToNodes(c),
  g.addToNodes(m),
  y.addToNodes(w),
  b.addToNodes(c),
  w.addToNodes(b),
  E.addToNodes(x),
  S.addToNodes(c),
  x.addToNodes(S);
let T = [];
T.push(f, l, c, d, h, p, g, v, m, w, y, b, x, E, S);

export default class HeadKv extends KvMotion {
  sounds = null;
  currentSound = null;
  initialize() {
    let e = this;
    e.initWithURL("views/head.xml"),
      (e.sounds = []),
      e.sounds.push(
        new AudioCtrl("sounds/head_1.mp3", !1),
        new AudioCtrl("sounds/head_2.mp3", !1),
        new AudioCtrl("sounds/head_3.mp3", !1),
        new AudioCtrl("sounds/head_4.mp3", !1)
      );
  }
  motionBox = null;
  motions = null;
  kvButtons = null;
  kvMovie = null;
  onViewControllerPrepared() {
    let e = this;
    (e.motionBox = e.view().subViewByName("motion_box")),
      (e.motions = e.motionBox.subViews().slice()),
      e.motionBox.empty(),
      (e.kvMovie = new Movie()),
      e.kvMovie.name("head_kv"),
      e.kvMovie.width(e.motionBox.width()),
      e.kvMovie.height(e.motionBox.height()),
      e.kvMovie.frameRate(20),
      e.kvMovie.loop(!1),
      e.motionBox.addSubView(e.kvMovie),
      EventCenter.addListener(e.kvMovie, e, Event.END, e.onKVMovieEnd),
      (e.kvButtons = new ButtonGroup()),
      (e.kvButtons.cancelable = !0),
      e.kvButtons.addButtons([
        e.view().subViewByName("btn_1"),
        e.view().subViewByName("btn_2"),
        e.view().subViewByName("btn_3"),
        e.view().subViewByName("btn_4"),
      ]),
      EventCenter.addListener(e.kvButtons, e, Event.CHANGE, e.onKVButtonsChange),
      (e.actions = []),
      e.currentNode(f);
  }
  onViewAddToStage() {
    let e = this;
    e.prepared &&
      ((e.actions = []),
      (e._currentNode = null),
      e.currentNode(f),
      e.kvButtons.cancelSelect());
  }
  onViewRemoveFromStage() {
    let e = this;
    e.prepared &&
      e.currentSound != null &&
      (e.currentSound.pause(), (e.currentSound = null));
  }
  _currentNode = null;
  currentNode(e) {
    let t = this;
    if (Boolean(e)) {
      let n = t._currentNode;
      t._currentNode = e;
      let r = null;
      n != null && n.name == t._currentNode.name
        ? ((r = t._currentNode.getMotionFrames(t.motions)),
          n.reversion == t._currentNode.reversion
            ? (r = r.slice(t.kvMovie.currentFrame(), r.length))
            : (r = r.slice(
                r.length - (t.kvMovie.currentFrame() - 1),
                r.length
              )))
        : (r = t._currentNode.getMotionFrames(t.motions)),
        r.length <= 0
          ? t.onKVMovieEnd()
          : (t.kvMovie.frames(r), t.kvMovie.goToAndPlay(0));
    }
    return t._currentNode;
  }
  actions = null;
  onKVMovieEnd() {
    let e = this,
      t = null;
    if (e.actions.length > 0) {
      t = e.actions.shift();
      let n = t.soundEffect();
      n != null &&
        (e.currentSound != null &&
          (e.currentSound.pause(), (e.currentSound = null)),
        (e.currentSound = n),
        e.currentSound.play(0));
    } else t = e._currentNode;
    (e._currentNode = null), e.currentNode(t);
  }
  onKVButtonsChange() {
    let e = this;
    e.currentSound != null && (e.currentSound.pause(), (e.currentSound = null));
    let t = null;
    switch (e.kvButtons.selectedIndex()) {
      case -1:
        t = f;
        break;
      case 0:
        t = d;
        break;
      case 1:
        t = g;
        break;
      case 2:
        t = w;
        break;
      case 3:
        t = x;
    }
    (e.actions = e.createPathFromNodeName(e._currentNode.name, t)),
      e.currentNode(e.actions.shift());
  }
  createPathFromNodes(e, t) {
    let n = this,
      r = null,
      i = e.toNodes.slice();
    while (r == null && i.length > 0) {
      let s = null,
        o = -1;
      for (let u = 0; u < i.length; u++) {
        let a = i[u];
        if (a == t) {
          r = [e, a];
          break;
        }
        if (s == null || s.distance(t) > a.distance(t)) (s = a), (o = u);
      }
      if (r == null) {
        i.splice(o, 1);
        let f = n.createPathFromNodes(s, t);
        f != null && (r = [e].concat(f));
      }
    }
    return r;
  }
  createPathFromNodeName(e, t) {
    let n = this,
      r = null;
    for (let i = 0; i < T.length; i++) {
      let s = T[i];
      if (s.name == e) {
        let o = n.createPathFromNodes(s, t);
        if (r == null || o.length < r.length) r = o;
      }
    }
    return r;
  }
}
